<?php
namespace app\index\controller;
use app\index\model\Login_model;
use think\Controller;
use think\Log;
use think\Request;
use think\Session;
Class Login extends Controller{

    function  __construct(Request $request = null)
    {
        parent::__construct($request);
    }

    function  index(){
        if(session('user')){
            $this->redirect('admin/admin/index');
        }else{
            if (isset($_GET['msg'])){
                $this->assign('msg',$_GET['msg']);
                unset($_GET['msg']);
            }else{
                $this->assign('msg','');
            }
            return $this->fetch();
        }

    }

    /**
     *
     */
    function  check()
    {
        if(!captcha_check($_POST['yzl'])){
            $this->redirect('index/login/index?&msg=验证码错误');
        }
        $login=new Login_model();
        $user=$login->user_select($_POST['username']);
        if(!empty($user)) {
            if ($user['password']==$_POST['password']){
                session('user',$user);
                Log::record(array('yzl'=>$user));
                Log::save();
                $this->redirect('admin/admin/index');

            }else {
                $this->redirect('index/login/index?&msg=密码错误');
            }
        }else{
            $this->redirect('index/login/index?&msg=账号不存在');
        }
    }
    function  login_out()
    {
        session('user',null);
        $this->redirect('index/login/index?&msg=已退出');
    }

}